home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / SOUND / AWECTRL.ZIP / EXAMPLE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-05  |  4.7 KB  |  173 lines

  1.     /**************************************************/
  2.     /*     An example of EMU8000 init and control     */
  3.     /*       Version 1.0   (c) Grinus/ToM, 1996       */
  4.     /*         Written for Borland C++ 3.1              */
  5.     /**************************************************/
  6.  
  7. /*
  8.   This little proggie shows the direct control of EMU8000 wave-table
  9.   synthesizer found on Sound Blaster AWE32 compatible sound cards.
  10.  
  11.   The following steps are taken:
  12.     1. A complete initialization of the EMU chip
  13.     2. A sample is sent to the on-board sample memory
  14.     3. A note is started
  15.     4. Various parameters of the sounding note are modified
  16.     5. The note is released
  17.     6. The EMU is reset to the default state (to provide FM effects)
  18.  
  19.   To hear the note, a reasonable setting of SB mixer is expected.
  20. */
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #include "awe.h"
  27.  
  28. // The definition of a sample (16-bit signed PCM sample)
  29. #define FILENAME "STRINGS.RAW"
  30. #define LOOPS     5112
  31. #define LOOPE    15493
  32. #define LENGTH   (LOOPE+ANTICLICK)
  33.  
  34. // The basic sample rate 22050Hz (-5 octaves from 176400Hz)
  35. #define    AWEPITCH  (0x10000L -5 * 0x1000)
  36. #define    LINPITCH  (0x10000L >> 5)
  37.  
  38. // --------------------------------------------------------------------- //
  39.  
  40. extern WORD Port620;    // EMU base port
  41.  
  42. // --------------------------------------------------------------------- //
  43.  
  44. WORD  AweRAMsize;    // [kB]
  45. WORD *buf = NULL;    // ptr to a buffer for sample data
  46. FILE *f   = NULL;    // for the sample file
  47.  
  48. // --------------------------------------------------------------------- //
  49.  
  50. void Err(char *s)    // abort with an error message
  51. {
  52.  if (f)            // a file is open?
  53.     fclose(f);
  54.  
  55.  if (buf)        // a memory is allocated?
  56.     free(buf);
  57.  
  58.  printf("ERROR: %s !\n", s);
  59.  exit(1);
  60. }
  61.  
  62.  
  63. void ReadSample()
  64. {
  65.  // Allocate buf[]
  66.  if (!(buf = (WORD *)malloc(LENGTH*2)))
  67.     Err("Can't allocate a buffer");
  68.  
  69.  // Load the sample to buf[]
  70.  if (!(f=fopen(FILENAME, "rb")))
  71.     Err("Can't open the file " FILENAME );
  72.  if (fread(buf, 2, LENGTH, f) != LENGTH)
  73.     Err("Can't read the sample file");
  74.  fclose(f); f = NULL;
  75. }
  76.  
  77.  
  78. // --------------------------  M A I N  ----------------------------- //
  79.  
  80. int main()
  81. {
  82. int   gChan = 0;    // let's use the first of 30 available channels
  83.  
  84.  puts("\nThe example of EMU8000 control.  Copyright (c) Grinus/ToM, 1996.\n");
  85.  
  86.  if (!AweDetect() )        // find EMU8000 chip
  87.     Err("EMU8000 not found");
  88.  
  89.  if (!(AweRAMsize = AweInitHw())) // error or no sample RAM ?
  90.     Err("EMU8000 init error");
  91.  
  92.  atexit( AweTerminate );    // setup the EMU clean-up routine
  93.  
  94.  // the start of main code
  95.  
  96.  // Report the configuration
  97.  printf("EMU8000 found at port 0x%X with %d KB of sample RAM.\n",
  98.              Port620, AweRAMsize );
  99.  
  100.  // Set the EMU8000 internal equalizer
  101.  AweTrebleBass(5, 5);        // 0dB, 0dB
  102.  
  103.  // Set the reverb and chorus types
  104.  AweReverbType(0);        // Room 1
  105.  AweChorusType(3);        // Chorus 4
  106.  
  107.  // Read a sample file
  108.  ReadSample();
  109.  
  110.  // Send the sample to the sample RAM
  111.  AweEnableRam(0);
  112.  AweWrD(AWE_WrAdrD, TOPRAM >> 16, TOPRAM & 0xFFFF );    // set address
  113.  AweWrBlock(buf, LENGTH);                // send buf[]
  114.  AweDisableRam();
  115.  
  116.  // Start a note
  117.  AweNoteOn( gChan,
  118.         6 * 16 / 6,        // Volume = -6 dB
  119.         0,            // Panning = left
  120.         25 * 256 / 100,    // Reverb = 25%
  121.         4 * 256 / 100,    // Chorus = 4%
  122.         AWEPITCH,        // AwePitch
  123.         LINPITCH,        // AweLinPitch
  124.         TOPRAM,          // Starting addr in the sample memory
  125.         TOPRAM+LOOPS,    // LoopS
  126.         TOPRAM+LOOPE    // LoopE
  127.       );
  128.  puts("A note started.");
  129.  
  130.  // Wait for 2 sec
  131.  AweWait(44100); AweWait(44100);
  132.  
  133.  // Change the volume
  134.  AweWrW( AWE_FC_Vol |gChan, 0xFF00 |           // FilterCutOff = max
  135.                 15 * 16 / 6 );    // Volume = -15 dB
  136.  puts("The volume decreased.");
  137.  
  138.  // Wait for 2 sec
  139.  AweWait(44100); AweWait(44100);
  140.  
  141.  // Make a pitch & panning slide
  142.  puts("The pitch and panning slide started.");
  143.  for (int i=0; i<64; i++) {
  144.     BYTE pan = 4 * i;            // slide from very left to right
  145.     WORD pitch = AWEPITCH + 64 * i;    // slide-up by one octave
  146.     DWORD l;                // temp. storage
  147.  
  148.     // Change the pitch
  149.     AweWrW( AWE_Pitch |gChan, pitch );        // new Pitch
  150.  
  151.     // Change the panning
  152.     l = AweRdD( AWE_DP_Rev_Pan |gChan);
  153.     AweWrD( AWE_DP_Rev_Pan |gChan, HWORD(l),    // old DestPitch
  154.                ((WORD)l & 0xFF00)    // old Reverb
  155.                | pan );        // new RightPan
  156.     l = AweRdD( AWE_Pan_Loops |gChan );
  157.     AweWrD( AWE_Pan_Loops |gChan,
  158.                ~(WORD)pan <<8    // new LeftPan
  159.                | (BYTE)HWORD(l), (WORD)l );// old LoopS
  160.     // Wait for 1/64 sec
  161.     AweWait(44100/64);
  162.     }
  163.  
  164.  // Wait for 1 sec
  165.  AweWait(44100);
  166.  
  167.  // Release a note in gChan
  168.  NoteOff(gChan);
  169.  puts("The note released.");
  170.  
  171.  return 0;
  172. }
  173.